﻿package server;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.sql.ResultSet;
import java.sql.SQLException;

public class ComHouseList extends UnicastRemoteObject implements ComHouseInterface{
	private Data data;
	public int houseNum;
	public String kind;
	public String key;
	public int ownID;
	
	public ComHouseList() throws RemoteException{
		data=new Data();
	}
	
	public String getKind(int id)throws RemoteException{
		String sql;
		ResultSet rs=null;
		String kind="";
		
		sql="select * from comhouselist where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				kind=rs.getString("houseClassify");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
		return kind;
	}
	


	public int getOwnID(int id)throws RemoteException{
		String sql;
		ResultSet rs=null;
		int ownerId=0;
		
		sql="select * from comhouselist where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				ownerId=rs.getInt("onwerId");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
		return ownerId;

	}
	

	public String getKey(int id)throws RemoteException{
		String sql;
		ResultSet rs=null;
		String key=null;
		
		sql="select * from comhouselist where id=" + id;
		rs=data.getResultSet(sql);
		try {
			while(rs.next()){
				key=rs.getString("ckey");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
		return key;
	}
	

	public boolean setKey(int id,String k) throws RemoteException{
		String sql;
		int re=0;
		
		sql="update comhouselist set ckey = '"+k+"' where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public boolean setOwnID(int id,int oi) throws RemoteException{
		String sql;
		int re=0;
		
		sql="update comhouselist set ownerId = "+oi+" where id="+id;
		re=data.update(sql);
		
		if(re==1){
			return true;
		}
		return false;
	}
	
	public int getSize() throws RemoteException{
		String sql;
		ResultSet rs=null;
		int sum=0;
		
		sql="select count(*) as sum from comhouselist";
		rs=data.getResultSet(sql);
		try {
			if(rs.next()){
				sum=rs.getInt("sum");
		}
			} catch (SQLException e) {
		
			e.printStackTrace();
			}
		return sum;
	}
}
